package my.study.org.mongodb.examples;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.UnknownHostException;

import my.study.org.mongodb.constant.MongoDBConstant;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class GridFSOpt {
	private Mongo connection;
	private DB db;
	private DBCollection collection = null;
	private GridFS myFS;

	private String mongoDBHost = "127.0.0.1";
	private int mongoDBPort = 27017;
	private String dbName = "testGridfs";
	private String collectionName = "fs";

	private static Log logger = LogFactory.getLog(GridFSOpt.class);
	
	public GridFSOpt(String mongoDBHost, int mongoDBPort, String dbName,
			String collectionName) throws UnknownHostException, MongoException {
		this.mongoDBHost = mongoDBHost;
		this.mongoDBPort = mongoDBPort;
		this.dbName = dbName;
		this.collectionName = collectionName;
		_init();
	}

	private void _init() throws UnknownHostException, MongoException {
		connection = new Mongo(mongoDBHost, mongoDBPort);
		db = connection.getDB(dbName);
		collection = db.getCollection(collectionName);
		myFS = new GridFS(db);
	}

	/**
	 * 用给出的id，保存文件，透明处理已存在的情况 id 
	 * 可以是string，long，int，org.bson.types.ObjectId 类型
	 * 
	 * @param in
	 * @param id
	 */
	public void save(InputStream in, Object id) {
		logger.info("save");
		DBObject query = new BasicDBObject("_id", id);
		GridFSDBFile gridFSDBFile = myFS.findOne(query);

		if (gridFSDBFile != null)
			return;

		GridFSInputFile gridFSInputFile = myFS.createFile(in, id.toString());
		gridFSInputFile.save();
		return;
	}

	/**
	 * 据id返回文件
	 * 
	 * @param id
	 * @return
	 */
	public GridFSDBFile getById(Object id) {
		DBObject query = new BasicDBObject("_id", id);
		GridFSDBFile gridFSDBFile = myFS.findOne(query);
		return gridFSDBFile;
	}

	/**
	 * 据文件名返回文件，只返回第一个
	 * 
	 * @param fileName
	 * @return
	 */
	public GridFSDBFile getByFileName(String fileName) {
		DBObject query = new BasicDBObject("filename", fileName);
		GridFSDBFile gridFSDBFile = myFS.findOne(query);
		return gridFSDBFile;
	}

	public static void main(String[] args) throws MongoException, IOException {
		GridFSOpt gridFSOpt = new GridFSOpt(MongoDBConstant.MONGO_HOST,
				MongoDBConstant.MONGO_PORT, MongoDBConstant.MONGO_DB_NAME, MongoDBConstant.MONGO_DB_COLLECTION);

		String fileName = "E:\\_diff.sh";
		String name = "_diff.sh";

		// 把文件保存到gridfs中，并以文件的md5值为id
		gridFSOpt.save(new FileInputStream(fileName), name);

		// 据文件名从gridfs中读取到文件

		GridFSDBFile gridFSDBFile = gridFSOpt.getByFileName(name);
		if (gridFSDBFile != null) {
			System.out.println("filename:" + gridFSDBFile.getFilename());
			System.out.println("md5:" + gridFSDBFile.getMD5());
			System.out.println("length:" + gridFSDBFile.getLength());
			System.out.println("uploadDate:" + gridFSDBFile.getUploadDate());

			System.out.println("--------------------------------------");
			gridFSDBFile.writeTo(System.out);
		} else {
			System.out.println("can not get file by name:" + name);
		}
	}
}
